package com.example.baselibs.utils.localsp;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.baselibs.BaseApplication;

import java.util.Map;

public class SPLocalSave {
    public static SPLocalSave shpLocalSave=null;
    SharedPreferences sharedPreferences;
    Context context;
    SharedPreferences.Editor editor;
    public SPLocalSave(String... str) {
        super();
        this.context= BaseApplication.getContext();
        if (str.length>0){
            switch (str.length){
                case 1:
                    sharedPreferences=context.getSharedPreferences(str[0],context.MODE_PRIVATE);
                    break;
                case 2:
                    sharedPreferences=context.getSharedPreferences(str[0],Integer.parseInt(str[1]));
                    break;
            }
        }

    }

    public SPLocalSave(Context context) {
        this.context=context;
        if (sharedPreferences==null){
            sharedPreferences=context.getSharedPreferences("wxb",context.MODE_PRIVATE);
        }
    }

    /**
     * 获取单例并创建指定文件名的xml文件
     * @param str str[0]:文件名，如果有存在，则复用；没有则创建,str[1]:文件mode，权限；
     * @return
     */
    public static SPLocalSave getInstance(String... str){
        if (shpLocalSave==null){
            synchronized (SPLocalSave.class){
                if (shpLocalSave==null){
                    shpLocalSave=new SPLocalSave(str);
                }
            }
        }
        return shpLocalSave;
    }

    /**
     * 默认创建wxb的xml文件
     * @return
     */
    public static SPLocalSave getInstance(){
        if (shpLocalSave==null){
            synchronized (SPLocalSave.class){
                if (shpLocalSave==null){
                    shpLocalSave=new SPLocalSave(BaseApplication.getContext());
                }
            }
        }
        return shpLocalSave;
    }

    /**
     * 获得编辑器
     * @return
     */
    private SharedPreferences.Editor getEditor(){
        editor=sharedPreferences.edit();
        return editor;
    }

    /**
     * 存boolean值
     * @param key
     * @param value
     */
    public void putBoolean(String key,boolean value){
        getEditor().putBoolean(key,value).apply();
    }

    /**
     * 如果获取的值为空，返回false
     * @param key
     * @return
     */
    public Boolean getBoolean(String key){
        return sharedPreferences.getBoolean(key,false);
    }

    public void putString(String key,String value){
        getEditor().putString(key,value).apply();
    }
    public String getString(String key){
        return sharedPreferences.getString(key,"this is null");
    }
    public void putLong(String key,long value){
        getEditor().putLong(key,value).apply();
    }
    public long getLong(String key){
        return sharedPreferences.getLong(key,0);
    }
    public void putInt(String key,int value){
        getEditor().putInt(key,value).apply();
    }
    public int getInt(String key){
        return sharedPreferences.getInt(key,0);
    }
    public void putFloat(String key,float value){
        getEditor().putFloat(key,value).apply();
    }
    public float getFloat(String key){
        return sharedPreferences.getFloat(key,0);
    }

    /**
     * 将XML文件转为map集合
     * @return
     */
    public Map getMap(){
        return sharedPreferences.getAll();
    }

    /**
     * 获取XML文件有多少条数据
     * @return
     */
    public int getMapSize(){
        return getMap().size();
    }

    /**
     * 删除指定数据
     * @param key
     */
    public void remove(String key){
        getEditor().remove(key).apply();
    }

    /**
     * 清空所有数据
     */
    public void clear(){
        getEditor().clear().apply();
    }
}
